package br.ufal.ic.core;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 * Entidade de guarda a pontuação de um competidor.
 *
 * @author Marcos Paulo
 */
@Entity
public class Score implements Serializable, Comparable<Score> {

    @Id
    private String id;
    @OneToOne
    private Competitor competitor;
    private int scoreValue;

    protected Score() {
        //for hibernate.
    }

    public Score(Competitor competitor) {
        if (competitor == null) {
            throw new IllegalArgumentException("o competidor não pode ser null");
        }

        this.id = competitor.getId();
        this.competitor = competitor;
        this.scoreValue = 0;

    }

    public Competitor getCompetitor() {
        return competitor;
    }

    public void setValue(int value) {
        if (value < 0) {
            throw new IllegalArgumentException("o valor do score tem que ser "
                    + "maior que zero.");
        }
        this.scoreValue = value;
    }

    public int getValue() {
        return scoreValue;
    }

    public int compareTo(Score score) {
        return new Integer(score.scoreValue).compareTo(scoreValue);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Score other = (Score) obj;

        return id.equals(other.id);
    }

    @Override
    public int hashCode() {
        return 97 * 7 + this.id.hashCode();
    }

//    public static void main(String[] args) {
//
//        //Competidor 1
//        Address address1 = new Address("doutor zeferino rodrigues", "Maceio",
//                "Brasil");
//        String name1 = "Marcos Paulo José de Melo Silva";
//        Date birth1 = new Date();
//        CubistInformation information1 = new CubistInformation(name1, address1, birth1);
//        Cube cube1 = new Cube(CubeType.CUBE_3X3X3);
//
//        Competitor competitor1 = new Competitor(information1, cube1);
//
//        Score score1 = new Score(competitor1);
//
//        //competidor 2
//        Address address2 = new Address("bebedouro", "Maceio", "Brasil");
//        String name2 = "Paulo José de Melo";
//        Date birth2 = new Date();
//        CubistInformation information2 = new CubistInformation(name2, address2,
//                birth2);
//        Cube cube2 = new Cube(CubeType.CUBE_3X3X3);
//
//        Competitor competitor2 = new Competitor(information2, cube2);
//
//        Score score2 = new Score(competitor2);
//
//        SortedSet<Score> scores = new TreeSet<Score>();
//
//        score1.setValue(100);
//        score2.setValue(200);
//
//        scores.add(score1);
//        scores.add(score2);
//
//        for (Score score : scores) {
//            System.out.println(score.getCompetitor().getId() + "-" + score.getValue());
//        }
//
//        score1.setValue(300);
//        scores = new TreeSet<Score>(new ArrayList<Score>(scores));
//
//        for (Score score : scores) {
//            System.out.println(score.getCompetitor().getId() + "-" + score.getValue());
//        }
////        System.out.println(scores);
//        System.out.println(score1.equals(score2));
//    }
}
